/* Version 0.01 2012.06.28  */
#include "screen.h"


Screen::Screen(float x, float y, int w, int h)
:Displayable(x, y, w, h)
{
	bCheck = TRUE;
	bClick = TRUE;
}

void Screen::setPos(float x, float y, float originalX, float originalY)
{
	Displayable::setPos(x, y);
	
	V_PSCREEN::iterator ps = vPScreen.begin();
	while(ps != vPScreen.end())
	{
		
		Screen* pd = *ps;
		pd ->setPos(pd->getX()-originalX+x, pd->getY()-originalY+y, pd->getX(), pd->getY());
		ps ++;
	}

	/*V_PDISPLAYTEXT::iterator pt = vPDisplayText.begin();
	while(pt != vPDisplayText.end())
	{
		
		DisplayText* pd = *pt;
		pd ->setPos(pd->getX()-originalX+x, pd->getY()-originalY+y);
		pt ++;
	}*/

	V_PBUTTON::iterator pi = vPButton.begin();
	while(pi != vPButton.end())
	{
		
		Button* pd = *pi;
		pd ->setPos(pd->getX()-originalX+x, pd->getY()-originalY+y);
		pi ++;
	}

}

Button* Screen::addButton(float _x, float _y, int _w, int _h, int marginX, int marginY, const char* text, int id)
{
	Button* pbtn = new Button(x+_x, y+_y, _w, _h, marginX, marginY, text, id);
	vPButton.push_back(pbtn);
	return vPButton.back();
}

Button* Screen::addButton(float _x, float _y, int _w, int _h, int marginX, int marginY, const char* text, int id, const char* mtrName)
{
	Button* pbtn = new Button(x+_x, y+_y, _w, _h, marginX, marginY, text, id, mtrName);
	vPButton.push_back(pbtn);
	return vPButton.back();
}
//void Screen::addText(float _x, float _y,  const wchar_t* text)
//{
//	DisplayText* pdt = new DisplayText(x+_x, y+_y, text);
//	vPDisplayText.push_back(pdt);
//
//}
//
//void Screen::addText(float _x, float _y,  NAME text)
//{
//	DisplayText* pdt = new DisplayText(x+_x, y+_y, text);
//	vPDisplayText.push_back(pdt);
//
//}
void Screen::addScreen(Screen& childScreen)
{
	childScreen.setPos(childScreen.getX()+x, childScreen.getY()+y, childScreen.getX(), childScreen.getY());
	vPScreen.push_back(&childScreen);
}
void Screen::draw(HDC hdc)
{
	Displayable::draw(hdc);
	
	V_PSCREEN::iterator ps = vPScreen.begin();
	while(ps != vPScreen.end())
	{
		
		Screen* pd = *ps;
		pd ->draw(hdc);
		ps ++;
	}

	/*V_PDISPLAYTEXT::iterator pt = vPDisplayText.begin();
	while(pt != vPDisplayText.end())
	{
		
		DisplayText* pd = *pt;
		pd ->draw(hdc);
		pt ++;
	}*/

	V_PBUTTON::iterator pi = vPButton.begin();
	while(pi != vPButton.end())
	{
		
		Button* pd = *pi;
		pd ->draw(hdc);
		pi ++;
	}
}

void Screen::checkArea(int mx, int my)
{
	if(isInArea(mx,my))
	{
		
		V_PSCREEN::iterator ps = vPScreen.begin();
		while(ps != vPScreen.end())
		{
			
			Screen* pd = *ps;
			pd ->checkArea(mx, my);
			ps ++;
		}

		
		V_PBUTTON::iterator pi = vPButton.begin();
		while(pi != vPButton.end())
		{
			
			Button* pd = *pi;
			pd ->checkArea(mx, my);
			pi ++;
		}
	}
}

int Screen::beClicked(int mx, int my)
{
	int btnID = -1;

	V_PSCREEN::iterator ps = vPScreen.begin();
	while(ps != vPScreen.end())
	{
		
		Screen* pd = *ps;
		
		if(pd ->isInArea(mx, my))
		{
			btnID = pd ->beClicked(mx, my);
			isClicked(btnID);
		}
		ps ++;
	}

	
	V_PBUTTON::iterator pi = vPButton.begin();
	while(pi != vPButton.end())
	{
		
		Button* pd = *pi;
		
		if(pd ->isInArea(mx, my))
		{
			btnID = pd ->beClicked();
			isClicked(btnID);
		}
		pi ++;
	}


	if(btnID >= 0)
		return btnID;
	else
		return TYPE_SCREEN;
}

